﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGS.Standard;
using System.Reflection;
using System.IO;
using System.Xml.Linq;
using SGS.Cards;
// using SGS.Rule;

using System.Xml;
using System.Xml.Schema;

namespace SGS
{

    public class DataConstructor
    {
    //    public static void test()
    //    {

    //        // Create the XmlSchemaSet class.
    //        XmlSchemaSet sc = new XmlSchemaSet();

    //        // Add the schema to the collection.
    //        sc.Add( "http://tempuri.org/CardName.xsd", @"E:\GameLoader2\GameLoader2\SGS\Resource\CardName.xsd" );

    //        // Set the validation settings.
    //        XmlReaderSettings settings = new XmlReaderSettings();
    //        settings.ValidationType = ValidationType.Schema;
    //        settings.Schemas = sc;
    //        settings.ValidationEventHandler += new ValidationEventHandler( ValidationCallBack );

    //        // Create the XmlReader object.
    //        XmlReader reader = XmlReader.Create( @"E:\GameLoader2\GameLoader2\SGS\Resource\CardNameMapping.xml", settings );

    //        // Parse the file. 
    //        while (reader.Read()) ;

    //    }

    //    // Display any validation errors.
    //    private static void ValidationCallBack( object sender, ValidationEventArgs e )
    //    {
    //        Console.WriteLine( "Validation Error: {0}", e.Message );
    //    }

    
        public static string ReadResourceXml( string resourceName )
        {
            var x = typeof( DataConstructor ).Assembly.GetManifestResourceStream( resourceName );
            using (StreamReader reader = new StreamReader( x ))
            {
                return reader.ReadToEnd();
            }
        }

        private static List<Card> ParseCardXml( string xml )
        {
            XElement parser = XElement.Parse( xml );
            List<Card> list = new List<Card>();

            foreach (var e in parser.Elements())
            {
                CardType type = (CardType)Enum.Parse( typeof( CardType ), e.Attribute( "Type" ).Value );
                Suit suit = (Suit)Enum.Parse( typeof( Suit ), e.Attribute( "Suit" ).Value );
                int point = Convert.ToInt32( e.Attribute( "Point" ).Value );

                Card c = null;

                switch (type)
                {
                    case CardType.Weapon:
                    case CardType.Armor:
                        c = SGS.Cards.Equipment.EquipmentBase.Create( type, e.Attribute( "RuntimeType" ).Value, suit, point );
                        break;
                    case CardType.AttackHorse:
                    case CardType.DefenseHorse:
                        c = SGS.Cards.Equipment.EquipmentBase.Create( type, e.Attribute( "RuntimeName" ).Value, suit, point );
                        break;
                    default:
                        c = new Card( suit, point, type );
                        break;
                }

                if (c != null)
                {
                    list.Add( c );
                }
            }

            return list;
        }

        public static CardCollection StandardCards
        {
            get
            {
                return new CardCollection( ParseCardXml( ReadResourceXml( "SGS.Resource.StandardCard.xml" ) ) );
            }
        }

        public static CardCollection MilitaryCards
        {
            get
            {
                return new CardCollection( ParseCardXml( ReadResourceXml( "SGS.Resource.MilitaryCard.xml" ) ) );
            }
        }

        public static CardCollection ExCards
        {
            get
            {
                return new CardCollection( ParseCardXml( ReadResourceXml( "SGS.Resource.ExCard.xml" ) ) );
            }
        }

        public static CardCollection CreateDefaultCardsForTest()
        {
            Card.ResetUniqueID();

            CardCollection coll = new CardCollection();


            coll.AddLast( new Card( Suit.Spade, 1, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 2, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 3, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 4, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 5, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 6, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 7, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 8, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 9, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 10, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 11, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 12, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Spade, 13, CardType.Bang ) );

            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 1 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 2 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 3 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 4 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 5 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 6 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 7 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 8 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 9 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 10 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 11 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 12 ) );
            coll.AddLast( new SGS.Cards.Equipment.Weapon.GuanShiFu( Suit.Club, 13 ) );
            /*coll.AddLast( new Card( Suit.Club, 2, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 3, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 4, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 5, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 6, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 7, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 8, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 9, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 10, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 11, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 12, CardType.Bang ) );
            coll.AddLast( new Card( Suit.Club, 13, CardType.Bang ) );*/

            coll.AddLast( new Card( Suit.Heart, 1, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 2, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 3, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 4, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 5, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 6, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 7, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 8, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 9, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 10, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 11, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 12, CardType.Miss ) );
            coll.AddLast( new Card( Suit.Heart, 13, CardType.Miss ) );

            coll.AddLast( new Card( Suit.Diamond, 1, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 2, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 3, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 4, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 5, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 6, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 7, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 8, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 9, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 10, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 11, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 12, CardType.Peach ) );
            coll.AddLast( new Card( Suit.Diamond, 13, CardType.Peach ) );

            return coll;
        }
    }
}
